


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Utility_Asset_XMissionStructures') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Utility_Asset_XMissionStructures
GO

create view v_tb_Utility_Asset_XMissionStructures as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Utility_Asset_XMissionStructureId] as [Utility_Asset_XMissionStructureId]
	,[UtilityFK] as [UtilityFK]
	,[Latitude] as [Latitude]
	,[Longitude] as [Longitude]
	,[Altitude] as [Altitude]
	,[SatelliteCount] as [SatelliteCount]
	,[SatelliteTime] as [SatelliteTime]
	,[SolutionAvailable] as [SolutionAvailable]
	,[Handheld] as [Handheld]
	,[XMissionStructures_TopTypeFk] as [XMissionStructures_TopTypeFk]
	,[Utility_ContactFK] as [Utility_ContactFK]
	,[Date] as [Date]
	,[Utility_ProjectFK] as [Utility_ProjectFK]
	,[HaveImages] as [HaveImages]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Utility_Asset_XMissionStructureId]), 255) as [FriendlyName]
from
	tb_Utility_Asset_XMissionStructures
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Test
GO


create proc up_UtilityAssetXMissionStructure_Test
	@Utility_Asset_XMissionStructureId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Utility_Asset_XMissionStructureId] 
	from [tb_Utility_Asset_XMissionStructures] source
	where [source].[Utility_Asset_XMissionStructureId] = @Utility_Asset_XMissionStructureId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Select
GO

create proc up_UtilityAssetXMissionStructure_Select
	 @Utility_Asset_XMissionStructureId int = null 
	,@UtilityFK int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Handheld nvarchar(100) = null 
	,@XMissionStructures_TopTypeFk int = null 
	,@Utility_ContactFK int = null 
	,@Date datetime = null 
	,@Utility_ProjectFK int = null 
	,@HaveImages int = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	 Utility_Asset_XMissionStructureId int
	,UtilityFK int
	,Latitude float
	,Longitude float
	,Altitude float
	,SatelliteCount int
	,SatelliteTime datetime
	,SolutionAvailable bit
	,Handheld nvarchar(100)
	,XMissionStructures_TopTypeFk int
	,Utility_ContactFK int
	,Date datetime
	,Utility_ProjectFK int
	,HaveImages int
	,[FriendlyName] varchar(255)
	)
	
insert @table
	(
	 [Utility_Asset_XMissionStructureId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[XMissionStructures_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[HaveImages]
	,[FriendlyName]
	)
select 
	 [Utility_Asset_XMissionStructureId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[XMissionStructures_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[HaveImages]
	,[FriendlyName]
from v_tb_Utility_Asset_XMissionStructures
where
		IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_Asset_XMissionStructureId], -1) = Coalesce(@Utility_Asset_XMissionStructureId, [v_tb_Utility_Asset_XMissionStructures].[Utility_Asset_XMissionStructureId], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[UtilityFK], -1) = Coalesce(@UtilityFK, [v_tb_Utility_Asset_XMissionStructures].[UtilityFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_XMissionStructures].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_XMissionStructures].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_XMissionStructures].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_XMissionStructures].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_XMissionStructures].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_XMissionStructures].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Handheld], '') = Coalesce(@Handheld, [v_tb_Utility_Asset_XMissionStructures].[Handheld], '')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[XMissionStructures_TopTypeFk], -1) = Coalesce(@XMissionStructures_TopTypeFk, [v_tb_Utility_Asset_XMissionStructures].[XMissionStructures_TopTypeFk], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_ContactFK], -1) = Coalesce(@Utility_ContactFK, [v_tb_Utility_Asset_XMissionStructures].[Utility_ContactFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Date], '1/1/1900 12:00AM') = Coalesce(@Date, [v_tb_Utility_Asset_XMissionStructures].[Date], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_ProjectFK], -1) = Coalesce(@Utility_ProjectFK, [v_tb_Utility_Asset_XMissionStructures].[Utility_ProjectFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[HaveImages], -1) = Coalesce(@HaveImages, [v_tb_Utility_Asset_XMissionStructures].[HaveImages], -1)

-- primary select
select * from @table





	-- UtilityAssetXMissionStructuresPoleCollection select

select
	v_tb_Utility_Asset_XMissionStructures_Poles.* 
from 
	v_tb_Utility_Asset_XMissionStructures_Poles 
	JOIN @table x ON v_tb_Utility_Asset_XMissionStructures_Poles.Utility_Asset_XMissionStructureFK = x.Utility_Asset_XMissionStructureId
	




	-- UtilityAssetXMissionStructuresImageCollection select

select
	v_tb_Utility_Asset_XMissionStructures_Images.* 
from 
	v_tb_Utility_Asset_XMissionStructures_Images 
	JOIN @table x ON v_tb_Utility_Asset_XMissionStructures_Images.Utility_Asset_XMissionStructureFK = x.Utility_Asset_XMissionStructureId
	

	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Select_Top1
GO

create proc up_UtilityAssetXMissionStructure_Select_Top1
	 @Utility_Asset_XMissionStructureId int = null 
	,@UtilityFK int = null 
	,@Latitude float = null 
	,@Longitude float = null 
	,@Altitude float = null 
	,@SatelliteCount int = null 
	,@SatelliteTime datetime = null 
	,@SolutionAvailable bit = null 
	,@Handheld nvarchar(100) = null 
	,@XMissionStructures_TopTypeFk int = null 
	,@Utility_ContactFK int = null 
	,@Date datetime = null 
	,@Utility_ProjectFK int = null 
	,@HaveImages int = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	  Utility_Asset_XMissionStructureId int
	, UtilityFK int
	, Latitude float
	, Longitude float
	, Altitude float
	, SatelliteCount int
	, SatelliteTime datetime
	, SolutionAvailable bit
	, Handheld nvarchar(100)
	, XMissionStructures_TopTypeFk int
	, Utility_ContactFK int
	, Date datetime
	, Utility_ProjectFK int
	, HaveImages int
	,[FriendlyName] varchar(255)
	)

insert @table
	(
	 [Utility_Asset_XMissionStructureId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[XMissionStructures_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[HaveImages]
	,[FriendlyName]
	)
select TOP 1
	 [Utility_Asset_XMissionStructureId]
	,[UtilityFK]
	,[Latitude]
	,[Longitude]
	,[Altitude]
	,[SatelliteCount]
	,[SatelliteTime]
	,[SolutionAvailable]
	,[Handheld]
	,[XMissionStructures_TopTypeFk]
	,[Utility_ContactFK]
	,[Date]
	,[Utility_ProjectFK]
	,[HaveImages]
	,[FriendlyName]
from v_tb_Utility_Asset_XMissionStructures
where
		IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_Asset_XMissionStructureId], -1) = Coalesce(@Utility_Asset_XMissionStructureId, [v_tb_Utility_Asset_XMissionStructures].[Utility_Asset_XMissionStructureId], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[UtilityFK], -1) = Coalesce(@UtilityFK, [v_tb_Utility_Asset_XMissionStructures].[UtilityFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Latitude], -1) = Coalesce(@Latitude, [v_tb_Utility_Asset_XMissionStructures].[Latitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Longitude], -1) = Coalesce(@Longitude, [v_tb_Utility_Asset_XMissionStructures].[Longitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Altitude], -1) = Coalesce(@Altitude, [v_tb_Utility_Asset_XMissionStructures].[Altitude], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SatelliteCount], -1) = Coalesce(@SatelliteCount, [v_tb_Utility_Asset_XMissionStructures].[SatelliteCount], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SatelliteTime], '1/1/1900 12:00AM') = Coalesce(@SatelliteTime, [v_tb_Utility_Asset_XMissionStructures].[SatelliteTime], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[SolutionAvailable], 0) = Coalesce(@SolutionAvailable, [v_tb_Utility_Asset_XMissionStructures].[SolutionAvailable], 0)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Handheld], '') = Coalesce(@Handheld, [v_tb_Utility_Asset_XMissionStructures].[Handheld], '')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[XMissionStructures_TopTypeFk], -1) = Coalesce(@XMissionStructures_TopTypeFk, [v_tb_Utility_Asset_XMissionStructures].[XMissionStructures_TopTypeFk], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_ContactFK], -1) = Coalesce(@Utility_ContactFK, [v_tb_Utility_Asset_XMissionStructures].[Utility_ContactFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Date], '1/1/1900 12:00AM') = Coalesce(@Date, [v_tb_Utility_Asset_XMissionStructures].[Date], '1/1/1900 12:00AM')
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[Utility_ProjectFK], -1) = Coalesce(@Utility_ProjectFK, [v_tb_Utility_Asset_XMissionStructures].[Utility_ProjectFK], -1)
	AND	IsNull([v_tb_Utility_Asset_XMissionStructures].[HaveImages], -1) = Coalesce(@HaveImages, [v_tb_Utility_Asset_XMissionStructures].[HaveImages], -1)

-- primary select
select * from @table


	-- UtilityAssetXMissionStructuresPoleCollection select

select
	v_tb_Utility_Asset_XMissionStructures_Poles.* 
from 
	v_tb_Utility_Asset_XMissionStructures_Poles 
	JOIN @table x ON v_tb_Utility_Asset_XMissionStructures_Poles.Utility_Asset_XMissionStructureFK = x.Utility_Asset_XMissionStructureId
	

	-- UtilityAssetXMissionStructuresImageCollection select

select
	v_tb_Utility_Asset_XMissionStructures_Images.* 
from 
	v_tb_Utility_Asset_XMissionStructures_Images 
	JOIN @table x ON v_tb_Utility_Asset_XMissionStructures_Images.Utility_Asset_XMissionStructureFK = x.Utility_Asset_XMissionStructureId
	
	
	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Insert
GO

create proc up_UtilityAssetXMissionStructure_Insert
	 @Utility_Asset_XMissionStructureId int =null OUTPUT  
	,@UtilityFK int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit  
	,@Handheld nvarchar(100) = null  
	,@XMissionStructures_TopTypeFk int = null  
	,@Utility_ContactFK int = null  
	,@Date datetime = null  
	,@Utility_ProjectFK int = null  
	,@HaveImages int = null  
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Utility_Asset_XMissionStructures 
	(
	
	 [UtilityFK] 
	,[Latitude] 
	,[Longitude] 
	,[Altitude] 
	,[SatelliteCount] 
	,[SatelliteTime] 
	,[SolutionAvailable] 
	,[Handheld] 
	,[XMissionStructures_TopTypeFk] 
	,[Utility_ContactFK] 
	,[Date] 
	,[Utility_ProjectFK] 
	,[HaveImages] 
	)
	values
	(
	
	 @UtilityFK 
	,@Latitude 
	,@Longitude 
	,@Altitude 
	,@SatelliteCount 
	,@SatelliteTime 
	,@SolutionAvailable 
	,@Handheld 
	,@XMissionStructures_TopTypeFk 
	,@Utility_ContactFK 
	,@Date 
	,@Utility_ProjectFK 
	,@HaveImages 
	)
set @Utility_Asset_XMissionStructureId = scope_identity()
select * from v_tb_Utility_Asset_XMissionStructures where Utility_Asset_XMissionStructureId = @Utility_Asset_XMissionStructureId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Update
GO


create proc up_UtilityAssetXMissionStructure_Update
	 @Utility_Asset_XMissionStructureId int  
	,@UtilityFK int = null  
	,@Latitude float = null  
	,@Longitude float = null  
	,@Altitude float = null  
	,@SatelliteCount int = null  
	,@SatelliteTime datetime = null  
	,@SolutionAvailable bit = null  
	,@Handheld nvarchar(100) = null  
	,@XMissionStructures_TopTypeFk int = null  
	,@Utility_ContactFK int = null  
	,@Date datetime = null  
	,@Utility_ProjectFK int = null  
	,@HaveImages int = null  
	,@OriginalUtility_Asset_XMissionStructureId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalUtility_Asset_XMissionStructureId is null
	set @OriginalUtility_Asset_XMissionStructureId = @Utility_Asset_XMissionStructureId

-- test
declare @Result bit
execute @Result = up_UtilityAssetXMissionStructure_Test @OriginalUtility_Asset_XMissionStructureId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_XMissionStructureId)
	raiserror(505060, 16, 1, 'UtilityAssetXMissionStructure', @x)
	return
end

-- proceed
update tb_Utility_Asset_XMissionStructures 
set

	 [UtilityFK] =  @UtilityFK
	,[Latitude] =  @Latitude
	,[Longitude] =  @Longitude
	,[Altitude] =  @Altitude
	,[SatelliteCount] =  @SatelliteCount
	,[SatelliteTime] =  @SatelliteTime
	,[SolutionAvailable] = IsNull(@SolutionAvailable, [source].[SolutionAvailable])
	,[Handheld] =  @Handheld
	,[XMissionStructures_TopTypeFk] =  @XMissionStructures_TopTypeFk
	,[Utility_ContactFK] =  @Utility_ContactFK
	,[Date] =  @Date
	,[Utility_ProjectFK] =  @Utility_ProjectFK
	,[HaveImages] =  @HaveImages
	
from tb_Utility_Asset_XMissionStructures source
where
	[source].[Utility_Asset_XMissionStructureId] = @OriginalUtility_Asset_XMissionStructureId


select * from v_tb_Utility_Asset_XMissionStructures where Utility_Asset_XMissionStructureId = @Utility_Asset_XMissionStructureId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_UtilityAssetXMissionStructure_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_UtilityAssetXMissionStructure_Delete
GO


create proc up_UtilityAssetXMissionStructure_Delete
	@Utility_Asset_XMissionStructureId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_UtilityAssetXMissionStructure_Test @Utility_Asset_XMissionStructureId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Utility_Asset_XMissionStructureId)
	raiserror(505060, 16, 1, 'UtilityAssetXMissionStructure', @x)
	return
end

-- proceed
delete from tb_Utility_Asset_XMissionStructures 
where [Utility_Asset_XMissionStructureId] = @Utility_Asset_XMissionStructureId

GO


